package com.mojang.mario.level;

import java.util.Random;


public class BgLevelGenerator
{
    private static Random levelRandom = new Random();

    public static Level createLevel(int width, int height, int type)
    {
        BgLevelGenerator levelGenerator = new BgLevelGenerator(width, height, type);
        return levelGenerator.createLevel(levelRandom.nextLong());
    }

    private int width;
    private int height;
    private int type;

    private BgLevelGenerator(int width, int height, int type)
    {
        this.width = width;
        this.height = height;
        this.type = type;
    }

    private Level createLevel(long rnd)
    {
        Level level = new Level(width, height);
        Random random = new Random(rnd);

        switch (type)
        {
        case LevelGenerator.SKY:
        {
            int oh = 0;
            int h = 0;
            for (int x = 0; x < width; x++)
            {
                oh = h;
                while (oh == h)
                {
                    h = random.nextInt(4)+2;
                }
                for (int y = 0; y < height; y++)
                {
                    int h0 = (oh < h) ? oh : h;
                    int h1 = (oh < h) ? h : oh;
                    if (y < h0)
                    {
                        int s = 2;
                        if (y < 2) s = y;
                        level.setByteLayer(x, y,0, (byte) (4 + s * 8));
                    }
                    else if (y == h0)
                    {
                        int s = h0 == h ? 0 : 1;
                        s += 2;
                        level.setByteLayer(x, y, 0,(byte) s);
                    }
                    else if (y == h1)
                    {
                        int s = h0 == h ? 0 : 1;
                        s += 2;
                        level.setByteLayer(x, y,0, (byte) (s + 16));
                    }
                    else
                    {
                        int s = y > h1 ? 1 : 0;
                        if (h0 == oh) s = 1 - s;
                        s += 2;
                        level.setByteLayer(x, y, 0,(byte) (s + 8));
                    }
                }
            }
            break;
        }
            case LevelGenerator.LANDSCAPE:
            {
                int oh = 0;
                int h = 0;
                for (int x = 0; x < width; x++)
                {
                    oh = h;
                    while (oh == h)
                    {
                        h = random.nextInt(4)+4;
                    }
                    
                    for (int y = 0; y < height; y++)
                    {
                    	
                        int h0 = (oh < h) ? oh : h;
                        int h1 = (oh < h) ? h : oh;
                        if (y < h0)
                        {
                           
                            level.setByteLayer(x, y,0, (byte) (5));
                        }
                        else if (y == h0)
                        {
                            int s = h0 == h ? 0 : 1;
                          
                            level.setByteLayer(x, y,0, (byte) s);
                        }
                        else if (y == h1)
                        {
                            int s = h0 == h ? 0 : 1;
                            
                            level.setByteLayer(x, y,0, (byte) (s + 16));
                        }
                        else
                        {
                            int s = y > h1 ? 1 : 0;
                            if (h0 == oh) s = 1 - s;
                            
                            level.setByteLayer(x, y,0, (byte) (s + 8));
                        }
                    }
                }
                break;
            }
            case LevelGenerator.TYPE_UNDERGROUND:
            {
                    int tt = 0;
                    for (int x = 0; x < width; x++)
                    {
                        if (random.nextDouble() < 0.75) tt = 1 - tt;
                        for (int y = 0; y < height; y++)
                        {
                            int t = tt;
                            int yy = y - 2;
                            if (yy < 0 || yy > 4)
                            {
                                yy = 2;
                                t = 0;
                            }
                            level.setByteLayer(x, y,0, (byte) (4 + t + (3 + yy) * 8));
                        }
                    }
             
                break;
            }
            case LevelGenerator.TYPE_CASTLE:
            {
               
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            int t = x % 2;
                            int yy = y - 1;
                            if (yy>2 && yy<5)
                            {
                                yy = 2;
                            }
                            else if (yy>=5)
                            {
                                yy-=2;
                            }
                            if (yy < 0)
                            {
                                t = 0;
                                yy = 5;
                            }
                            else if (yy > 4)
                            {
                                t = 1;
                                yy = 5;
                            }
                            else if (t<1 && yy==3)
                            {
                                t = 0;
                                yy = 3;
                            }
                            else if (t<1 && yy>0 && yy<3)
                            {
                                t = 0;
                                yy = 2;
                            }
                            level.setByteLayer(x, y,0, (byte) (1+t + (yy + 4) * 8));
                        }
                    }
                }

        }
        return level;
    }
}